/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { Report } from '../../../common/report/report.js';
import { ReportListener } from '../../../common/report/report-listener.js';
import { ReportServiceImpl } from '../../../common/report/report-service-impl.js';
import { LogType } from '../enum/log-type.js';

import { Log, ScheduleLog, WorkLog } from './log.js';
import { LogCollector } from './log-collector.js';
import { LogService } from './log-service.js';

/**
 * LogService实现类
 *
 * @since 2022/8/18
 */
export class LogServiceImpl implements LogService, ReportListener {
  private isActive: boolean;
  private logCollectorMap: Map<LogType, LogCollector<Log>>;

  constructor() {
    this.isActive = true;
    this.logCollectorMap = new Map();
    this.logCollectorMap.set(LogType.SCHEDULE, new LogCollector<ScheduleLog>());
    this.logCollectorMap.set(LogType.WORK, new LogCollector<WorkLog>());
    ReportServiceImpl.getInstance().addListener(this);
  }

  clear(type?: LogType): void {
    if (type) {
      this.logCollectorMap.get(type)?.clear();
    } else {
      this.logCollectorMap.clear();
    }
  }

  getLog(type: LogType): Set<Log> | undefined {
    return this.logCollectorMap.get(type)?.dump();
  }

  /**
   * 添加日志
   *
   * @param log 待添加的日志
   */
  addLog(log: Log): void {
    if (this.isActive) {
      this.logCollectorMap.get(log.type)?.add(log);
    }
  }

  /**
   * 允许收集日志
   */
  start(): void {
    this.isActive = true;
  }

  /**
   * 停止收集日志
   */
  end(): void {
    this.isActive = false;
  }

  queryReport(): Report<Set<Log>> {
    return new Report(`${LogType.WORK}Log`, new Set<WorkLog>());
  }
}
